#! /use/bin/env python
# -*- coding:utf-8 -*-

# 2018-1-14
# python3
# 需要模块 urllib re, bs4

__author__ = 'Liyy'

import urllib,re
from urllib import request
from bs4 import BeautifulSoup




def getContentOrComment(url):
    headers = {'User-Agent':'User-Agent:Mozilla/5.0 (Machintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
    try:
        req = request.Request(url, headers=headers)
        a = request.urlopen(req).read()
        msg = a.decode('utf-8')
    except Exception as e:
        msg = None
    # print(msg)
    return msg


# 文章
articlurl = "https://www.qiushibaike.com/textnew/page/%d"
# 评论
commenturl = "https://www.qiushibaike.com/article/%d"

page =0
# Url = articlurl % page

while True:
    raw = input("点击enter查看或者输入exit退出：")
    if raw == "exit":
        break;
    page +=1
    Url = articlurl % page
    articlePage = getContentOrComment(Url)
    if articlePage==None:
        continue
    articleFloor = 1
    articles = BeautifulSoup(articlePage, 'html.parser')  # 解析

    for string in articles.find_all(attrs='article block untagged mb15'):
        comm = str(string.get('id'))
        id = comm.strip()[11:]
        print('%d: \n  %s' % (int(id),string.find(attrs='content').get_text().strip()))
        articleFloor += 1
        arturl = commenturl % int(id)
        print(arturl)
        article = getContentOrComment(arturl)
        if article == None:
            continue
        # print(article)  #爬取文章
        content = BeautifulSoup(article, 'html.parser')
        commentFloor = 1
        for strings in content.find_all(attrs='body'):
            print('%d 楼: %s' % (commentFloor,strings.get_text().strip()))  # 获取文本内容 并清除空格
            commentFloor += 1


